<template>
  <div class="app-container">
    <div>
        <h3>版本信息</h3>
        <p>当前版本：{{ getVersion() }}</p>
         <p>版本说明：{{ getDesc()}}</p>
    </div>
    <el-button type="primary" size="small" @click="centerDialogVisible = true">检查更新</el-button>
    <el-button type="primary" size="small" @click="addUpdate">新建</el-button>
    <el-dialog
      :title="title"
      :visible.sync="centerDialogVisible"
      width="40%"
      center>
      <span>{{ remark }}</span>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" type="primary" @click="updateApp">立即升级</el-button>
        <el-button size="small" @click="centerDialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
    <el-dialog
      :visible.sync="downloadDialogVisible"
       show-close="false" 
       close-on-click-modal="false" 
       close-on-press-escape="falses" 
       title="下载进度"
      width="30%"
      >
      <div style="text-align:center">
          <el-progress type="circle" :percentage="downloadPercent"></el-progress>
      </div>
    </el-dialog>
    <el-dialog
      class="closeMainWinDialog"
      :visible.sync="closeMainWinDialogVisible"
       title="关闭提示"
       width="30%"
      >
        <img src="@/page/main/assets/mark64.png" alt="" style="float:left;margin-right:30px;">
      <div>
        <p>你点击了关闭按钮，你是想：</p>
         <el-radio-group v-model="closeWinSelect">
          <el-radio :label="0" style="margin-bottom:10px">最小化到托盘，不退出程序</el-radio><br>
          <el-radio :label="1">关闭</el-radio>
        </el-radio-group>
        <!-- <el-radio v-model="closeWinSelect" label="0">备选项</el-radio><br>
        <el-radio v-model="closeWinSelect" label="1">备选项</el-radio> -->
      </div>
      <el-checkbox v-model="closeWinSelectChecked">不再提示</el-checkbox>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeMainWinDialogVisible = false">取 消</el-button>
        <el-button size="small" type="primary" @click="confirmCloseWinDialog">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { ipcRenderer } from 'electron'
import { getVersion, getDesc } from '@/page/main/utils/sys'
export default {
  data() {
    return {
      downloadPercent: '',
      centerDialogVisible: false,
      downloadDialogVisible: false,
      closeMainWinDialogVisible: false,
      title: '最新版本3.3.1',
      remark: '需要注意的是内容是默认不居中的',
      closeWinSelect: 0
    }
  },
  mounted() {
    this.checkForUpdate()
    this.getCommandColse()
  },
  methods: {
    getCommandColse() {
      ipcRenderer.on('closeMainWin', () => {
        console.log(this.closeWinSelect)
        this.closeMainWinDialogVisible = true
      })
    },
    confirmCloseWinDialog() {
      this.closeMainWinDialogVisible = false
      if (this.closeWinSelect === 0) {
        ipcRenderer.send('minimizeCloseWin')
      } else {
        ipcRenderer.send('closeMainWin')
      }
    },
    getVersion() {
      return getVersion()
    },
    getDesc() {
      return getDesc()
    },
    updateApp() {
      this.centerDialogVisible = false
      this.downloadDialogVisible = true
      this.downloadUpdate()
    },
    checkForUpdate() {
      ipcRenderer.send('checkForUpdate')
    },
    downloadUpdate() {
      ipcRenderer.send('downloadUpdate')
      // 注意："downloadProgress”事件可能存在无法触发的问题，只需要限制一下下载网速就好了
      ipcRenderer.on('downloadProgress', (event, progressObj) => {
        console.log(progressObj)
        this.downloadPercent = progressObj.percent.toFixed(2) || 0
        console.log(Math.trunc(this.downloadPercent))
        console.log(Math.trunc(this.downloadPercent) === 100)
        if (Math.trunc(this.downloadPercent) === 100) {
          // console.log('开始更新...')
        }
      })
    },
    addUpdate() {
      ipcRenderer.send('addCloseWin')
    }
  },
  destroyed() {
    // ipcRenderer.removeAll(['message', 'downloadProgress'])
  }
}
</script>


